#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"msgList.h"
using namespace std;


msg_recv * msgRecvInit(unsigned int len, char * buffIn,unsigned int peerIpIn, unsigned short peerPort,int fd)
{
     msg_recv * ptrMsg = (msg_recv *)malloc(sizeof(msg_recv));
     if(ptrMsg == NULL)
     {
         return NULL;
     }
     ptrMsg->peerIp = peerIpIn;
	 ptrMsg->peerPort = peerPort;
	 ptrMsg->fd = fd;
     ptrMsg->msgLen = (len>100)?100:len;
     memset(ptrMsg->buff,0,100);
     memcpy(ptrMsg->buff,buffIn,ptrMsg->msgLen);
     
     return ptrMsg;
}

void msgRecvFree(msg_recv * ptrMsg)
{
    memset(ptrMsg,0,sizeof(msg_recv));
}

CRecvPool::CRecvPool()
{
    pthread_mutex_init(&mutexList, NULL);
	pthread_mutex_init(&mutexList_Send, NULL);
	
    if(msgList.size())
    {
        for(iterList=msgList.begin();iterList!=msgList.end();iterList++)
        {
            free(*iterList);
        }
    }
    msgList.clear();

	if(msgListSend.size())
    {
        for(iterList=msgListSend.begin();iterList!=msgListSend.end();iterList++)
        {
            free(*iterList);
        }
    }
    msgListSend.clear();
}


CRecvPool::~CRecvPool()
{
    pthread_mutex_destroy(&mutexList);
	pthread_mutex_destroy(&mutexList_Send);
    if(msgList.size())
    {
        for(iterList=msgList.begin();iterList!=msgList.end();iterList++)
        {
            free(*iterList);
        }
    }
    msgList.clear();

	if(msgListSend.size())
    {
        for(iterList=msgListSend.begin();iterList!=msgListSend.end();iterList++)
        {
            free(*iterList);
        }
    }
    msgListSend.clear();
}

int CRecvPool::addOneMsg(unsigned int len, char * buff, unsigned int peerIp,unsigned short peerPort,int fd,int isRecv)
{
    msg_recv * msgNew = msgRecvInit(len, buff,peerIp,peerPort,fd);
    if(msgNew==NULL)
    {
       return 1;
    }

	if(isRecv)
	{
	    pthread_mutex_lock(&mutexList);
        msgList.push_back(msgNew);
        pthread_mutex_unlock(&mutexList);
	}
	else
	{
	    pthread_mutex_lock(&mutexList_Send);
        msgListSend.push_back(msgNew);
        pthread_mutex_unlock(&mutexList_Send);
	}
    
    return 0;
}

msg_recv * CRecvPool::getOneMsg(int isRecv)
{
   msg_recv * tempMsg = NULL;
   if(isRecv)
   {
       if(msgList.size() == 0)
		{
		    return NULL;
		}

		tempMsg = *(msgList.begin());

		pthread_mutex_lock(&mutexList);
		msgList.pop_front();
		pthread_mutex_unlock(&mutexList);
   }
   else
   {
        if(msgListSend.size() == 0)
		{
		    return NULL;
		}

		tempMsg = *(msgListSend.begin());

		pthread_mutex_lock(&mutexList_Send);
		msgListSend.pop_front();
		pthread_mutex_unlock(&mutexList_Send);
   }  
   
   return tempMsg;
}

int main1(int argc, char * argv[])
{
    unsigned int lenBuff = 10;
    unsigned int peerIp = 0x56781020;
    char buff[20] = "12345678";
    int ret = 0;

    CRecvPool * recvPool = new CRecvPool();
    
    ret = recvPool->addOneMsg(lenBuff,buff,peerIp,87,-1,1);
    if(!ret)
    {
         cout<<"add one Msg"<<endl;
    }   
    
    msg_recv * oneMsg = recvPool->getOneMsg(1);
    
    if(oneMsg)
    {
      printf("getMsg success: len[%d],buff[%s],peerIp[%x]\n",oneMsg->msgLen,oneMsg->buff,oneMsg->peerIp);  
    }
    
    memset(oneMsg,0,sizeof(msg_recv));
    free(oneMsg);
    delete recvPool;
    
    return 0;
}

CRecvPool * g_ptrRecvPool = NULL;

